package socdev.mobile;

import socdev.data.Message;
import socdev.data.Project;
import socdev.data.ProjectContainer;
import socdev.data.User;
import socdev.mobile.TasksActivity.HandlerRunnable;
import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ProjectActivity extends Activity {

	private static ProjectContainer projects;
	private static ListView projectList;
	private static Context context;
	private static User user;
	private Dialog projectDialog;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.projects);
        
        user = (User) getIntent().getExtras().get("user");
        projects = new ProjectContainer(user);
        
        
        projectList = (ListView) findViewById(R.id.project_list);
        context = this;
        
        
        //projectList.setAdapter(new ProjectsAdapter(this, R.layout.project_item, projects.getAll()));
        projectList.setTextFilterEnabled(true);
        
        Handler handler = new Handler();
	    
        projects.update(handler, new HandlerRunnable());
        projectList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	Project project = projects.getAll().get(position);
            	if (project.isMember()) {
            		Intent myIntent = new Intent(view.getContext(), SocDevActivity.class);
            		myIntent.putExtra("user", user);
            		myIntent.putExtra("project", projects.getAll().get(position));
            	
            		startActivityForResult(myIntent, 0);
            	}
            }

          });
        
        
        
        projectDialog = new Dialog(context);

        projectDialog.setContentView(R.layout.join_project_dialog);
        projectDialog.setTitle("Připojit se k projektu");
        
        
        Button projectJoin = (Button) projectDialog.findViewById(R.id.join);
        
        projectJoin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	
            	EditText projectName = (EditText)projectDialog.findViewById(R.id.project_name);
            	
            	TextView info =  (TextView) projectDialog.findViewById(R.id.info);
                info.setText("Projekt neexistuje");
                
                Project.join(user, projectName.getText().toString());
                	projects.update(new Handler(), new HandlerRunnable());
            }

        });
        
       
        
    }
	
	public void onResume() {
    	super.onResume();
    	projects.update(new Handler(), new HandlerRunnable());
    }
	
	static public class HandlerRunnable implements Runnable {
		
		
		public void run() {
			projectList.setAdapter(new ProjectsAdapter(context, R.layout.project_item, projects.getAll()));
		}
	
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.project_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
		switch(item.getItemId()) {
		case R.id.join_to_project:
			projectDialog.show();
			return true;
		}
	    return false;
	}
	

}
